﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FogOutlook {
	class DirectReport {
		internal Microsoft.Office.Tools.Ribbon.RibbonButton directbtn;
		internal Microsoft.Office.Tools.Ribbon.RibbonMenu directMenu;
		internal Microsoft.Office.Tools.Ribbon.RibbonLabel activeProject;

		public bool Visible {
			set {
				directbtn.Visible = value;
				directMenu.Visible = value;
				activeProject.Visible = value;
			}
		}

		internal DirectReport(Microsoft.Office.Tools.Ribbon.RibbonGroup group) {
			directMenu = Globals.Factory.GetRibbonFactory().CreateRibbonMenu();
			directbtn = Globals.Factory.GetRibbonFactory().CreateRibbonButton();
			activeProject = Globals.Factory.GetRibbonFactory().CreateRibbonLabel();
			group.Items.Add(directbtn);
			group.Items.Add(activeProject);
			group.Items.Add(directMenu);
			// 
			// directbtn
			// 
			//directMenu.ControlSize = Microsoft.Office.Core.RibbonControlSize.RibbonControlSizeLarge;
			directbtn.Image = global::FogOutlook.Properties.Resources.FogOutlook;
			directbtn.Label = String.Format("Add To", Environment.NewLine);
			directbtn.Name = "directbtn";
			directbtn.ShowImage = true;
			directbtn.Click += new Microsoft.Office.Tools.Ribbon.RibbonControlEventHandler(directbtn_Click);
			directbtn.Visible = false;
			// 
			// activeProject
			// 
			if(Properties.Settings.Default.directToProject.Length > 0) {
				activeProject.Label = Properties.Settings.Default.directToProject;
				activeProject.Tag = activeProject.Label;
			}
			else
				activeProject.Label = "<not set>";
			activeProject.Name = "activeProject";
			activeProject.Visible = false;
			// 
			// directMenu
			// 
			directMenu.Dynamic = true;
			directMenu.Label = "Project";
			directMenu.Name = "directMenu";
			directMenu.ItemsLoading += new Microsoft.Office.Tools.Ribbon.RibbonControlEventHandler(directMenu_ItemsLoading);
			directMenu.Visible = false;
			
		}

		void directMenu_ItemsLoading(object sender, Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs e) {
			if(!ThisAddIn.IsConnected) {
				if(!ThisAddIn.WaitConnect())
					return;
			}
			try {
				if(directMenu.Items.Count == 0) { // Only load if not loaded
					string[] projects = ThisAddIn.BugTracker.GetProjects();
					foreach(string p in projects) {
						Microsoft.Office.Tools.Ribbon.RibbonToggleButton btn = Globals.Factory.GetRibbonFactory().CreateRibbonToggleButton();
						btn.Label = p;
						btn.Tag = p;
						btn.Click += new Microsoft.Office.Tools.Ribbon.RibbonControlEventHandler(btn_Click);
						directMenu.Items.Add(btn);
					}

				}
			} catch(Exception x) {
				System.Diagnostics.Debug.WriteLine(x.Message);
			}
		}

		private bool CheckConnect() {
			if(!ThisAddIn.IsConnected) {
				return ThisAddIn.WaitConnect();
			}
			return true;
		}

		void directbtn_Click(object sender, Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs e) {
			if(activeProject.Label.Equals("<not set>")) {
				MessageBox.Show("No project set");
				return;
			}
			if(!CheckConnect()) {
				if(!ThisAddIn.TryConnect(false, Properties.Settings.Default.username, Properties.Settings.Default.password, Properties.Settings.Default.btURL, Properties.Settings.Default.bttype))
					return;
			}
//			foreach(base in directTo.Items) {

	//		}
			string project = Properties.Settings.Default.directToProject;
			if(project.Length <= 0) {
				System.Windows.Forms.MessageBox.Show("No project set for reporting directly to FogBugz. Set one from the drop down list by clicking it.", "No project set");
				return;
			}
			var explorer = ThisAddIn.app.ActiveExplorer();
			foreach(Object selObject in explorer.Selection) {
				Microsoft.Office.Interop.Outlook.MailItem mi = selObject as Microsoft.Office.Interop.Outlook.MailItem;
				if(mi != null) {
					ThisAddIn.BugTracker.DirectReport(project, mi, ThisAddIn.GetSenderSMTPAddress(mi), Properties.Settings.Default.includeAttachments);
				}
			}
		}

		void btn_Click(object sender, Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs e) {
		//	Microsoft.Office.Tools.Ribbon.RibbonToggleButton btn = sender as Microsoft.Office.Tools.Ribbon.RibbonToggleButton;
			//btn.Checked = !btn.Checked;
			foreach(Microsoft.Office.Tools.Ribbon.RibbonToggleButton btn in directMenu.Items)
					btn.Checked = false;
			((Microsoft.Office.Tools.Ribbon.RibbonToggleButton)sender).Checked = true;
			activeProject.Label = ((Microsoft.Office.Tools.Ribbon.RibbonToggleButton)sender).Tag.ToString();
			activeProject.Tag = activeProject.Label;
			Properties.Settings.Default.directToProject = activeProject.Label;
			Properties.Settings.Default.Save();
		}
	}
}
